package gui;

import org.Consts;
import org.Globals;
import org.TempVariables;
import org.json.JSONException;
import org.json.JSONObject;

import com.ydev.graphics.animations.R;

import CustomUI.EditPartView;
import CustomUI.ydButton;
import DTO.ObjectDTO;
import DTO.PartDTO;
import GameTools.ydPoint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class EditParts extends Activity {
	
	private String array_spinner[];
	 Spinner s;
	 EditPartView editPartView;
	 
	 EditText xValue;
	 EditText yValue;
	 
	 
	 ObjectDTO objDTO;
	 PartDTO partDTO;
	 
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_edit_parts);
        init();
	}
	
	public void init(){
		
		xValue=(EditText)findViewById(R.id.EditTextXValue);
		yValue=(EditText)findViewById(R.id.EditTextYValue);
		
		 s = (Spinner) findViewById(R.id.spinnerTypes);
		 editPartView = (EditPartView)findViewById(R.id.editPartView1);
		 array_spinner=new String[2];
	        array_spinner[0]="Pivot";
	        array_spinner[1]="Fixed";
	        ArrayAdapter adapter = new ArrayAdapter(this,
	        R.layout.spinner_item_custom, array_spinner);
	        s.setAdapter(adapter);
	        
	        s.setOnItemSelectedListener(new OnItemSelectedListener() {
	            @Override
	            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
	               String selected=((TextView)selectedItemView).getText().toString();
	               if(selected.equals("Fixed"))
	            	   editPartView.selectAnchorType(0);
	               else
	            	   editPartView.selectAnchorType(1);
	               return;
	            }

	            @Override
	            public void onNothingSelected(AdapterView<?> parentView) {
	                // your code here
	            }

	        });
	        
	        Bundle extras = getIntent().getExtras();
			String str=extras.getString("partImage");
			
			objDTO=new ObjectDTO(Integer.parseInt(str), Globals.sqlDB);
			partDTO = new PartDTO();
			
			partDTO.image=objDTO.image;
			editPartView.setImage(objDTO.image);
			
			editPartView.selectAnchorType(1);
			
		//	Drawable myDrawable = getResources().getDrawable(R.drawable.logo);
		//	Bitmap myLogo = ((BitmapDrawable) myDrawable).getBitmap();
			//editPartView.setAnchor(BitmapFactory.decodeResource(getResources(), R.drawable.anchor_fixed));
			editPartView.selectAnchorType(0);
			
			editPartView.OnTouchListeners.addOnTouchListener(new View.OnTouchListener() {	

				@Override
				public boolean onTouch(View v, MotionEvent event) {
					setCoordi();
					return true;
				}

	});
			
			ydButton btn;
			btn = (ydButton) (findViewById(R.id.ydButtonNW));
			setDirectionClickedListener(btn);
			btn = (ydButton) (findViewById(R.id.ydButtonN));
			setDirectionClickedListener(btn);
			btn = (ydButton) (findViewById(R.id.ydButtonNE));
			setDirectionClickedListener(btn);
			btn = (ydButton) (findViewById(R.id.ydButtonW));
			setDirectionClickedListener(btn);
			btn = (ydButton) (findViewById(R.id.ydButtonE));
			setDirectionClickedListener(btn);
			btn = (ydButton) (findViewById(R.id.ydButtonSW));
			setDirectionClickedListener(btn);
			btn = (ydButton) (findViewById(R.id.ydButtonS));
			setDirectionClickedListener(btn);
			btn = (ydButton) (findViewById(R.id.ydButtonSE));
			setDirectionClickedListener(btn);
			
			
			btn = (ydButton) (findViewById(R.id.ydButtonCheck));

			btn.addOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {

					
				}
			});

			btn = (ydButton) (findViewById(R.id.ydButtonX));

			btn.addOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					String x=xValue.getText().toString();
					String y=yValue.getText().toString();
					float xf=Float.parseFloat(x);
					float yf=Float.parseFloat(y);
					partDTO.OriginalSize.setCoordinates(partDTO.image.getWidth(), partDTO.image.getHeight());
					partDTO.Position.setCoordinates(0,0);
					partDTO.MainAnchor.Position.setCoordinates(xf,yf);
					TempVariables.partDTO=partDTO;
					Intent intent = new Intent();
					intent.putExtra("isChanged",true);
					setResult(Activity.RESULT_OK, intent);
					finish();
				}
			});
	}
	
	public void setDirectionClickedListener(ydButton btn) {

		btn.addOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				JSONObject obj = null;

				try {
					obj = new JSONObject((String) v.getTag());
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				String direction = obj.optString(Consts.VIEW_TAGS_DIRECTION);

				editPartView.directionClicked(direction);
				setCoordi();

			}
		});
	}
	
	public void setCoordi(){
		ydPoint a= editPartView.getAnochorLocation();
		xValue.setText((int)a.x +"");
		yValue.setText((int)a.y +"");
	}
	
	
}
